app.controller("z-rest-c", [ '$scope','$rootScope','$z-http','$sce','z-rest-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,MenuService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";
		$scope.editModel = {};
		$scope.editModel.title="";
		$scope.editModel.modifyLogs={};
		$scope.modifyLogsModel = {};
		$scope.modifyLogsModel.show=false;

		//日志按钮
		$scope.modifyLogsModel.logs = function(modifyLogs){
			$scope.modifyLogsModel.show=true;
			$scope.modifyLogsModel.isLoading = true;
			//获取基本信息
			Service.getModifyLogsList({
				employeeNo:modifyLogs.employeeNo,
			},
			function(data){
				$scope.modifyLogsModel.title = "操作日志";
				$scope.modifyLogsModel.dataList = data;
				$scope.modifyLogsModel.isLoading=false;
			},function(error) {
				$scope.modifyLogsModel.isLoading=false;
			});
		}
		//关闭加载
		$scope.modifyLogsModel.unLoad=function(){
			$scope.modifyLogsModel.show=false;
		};
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'rest'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载数据 TODO test
		$scope.viewModel.bindData = function(){
			$scope.viewModel.employeeList=[];
			if($scope.viewModel.employeeNo!=null){
				var employeeNoData = $scope.viewModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getRestBalanceList({
				employeeList:$scope.viewModel.employeeList,
				pageNo:$scope.viewModel.rowCount,
				pageCount:$scope.viewModel.perPage
			},
			function(data){
				$scope.viewModel.dataList = data.modifyLogsBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
//			$scope.editModel.user={};
		};
		
		// 将分钟数量转换为小时和分钟字符串
		$scope.viewModel.toHourMinute=function(minutes){
			if(minutes==null){
				return "";
			}
			return (Math.floor(minutes/60).toFixed(0) + "小时" + Math.floor(minutes%60).toFixed(0) + "分钟" );
		};
		//验证
		$scope.viewModel.validForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.afterBalanceHoursDay.$dirty){
            		$scope.editForm.afterBalanceHoursDay.$dirty=true;
            	}
                return true;
            }else{
            	return false;
            }
        };
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.validForm()){
				return;
			}
			$scope.editModel.modifyLogs.minutes=$scope.editModel.modifyLogs.afterBalanceHoursDay - $scope.editModel.modifyLogs.balanceHoursDay;
			$scope.viewModel.isLoading = true;
			Service.saveModifyLogs($scope.editModel.modifyLogs,
					function(data){
				$scope.viewModel.unLoad();
				$scope.viewModel.bindData();
				$rootScope.alertInfo("修改成功。");
				$scope.viewModel.isLoading = false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading = false;
			});
		};
		//新增按钮
		$scope.viewModel.add = function(modifyLogs){
			$scope.editModel.title="调休余额设置";
			$scope.editModel.modifyLogs=angular.copy(modifyLogs);
			//验证不能为空
			$scope.editForm.afterBalanceHoursDay.$dirty=false;
			$scope.viewModel.load();
		};
		//查看日志
		$scope.viewModel.logs = function(){
			
		};
		
		$scope.viewModel.dirty=function(){
			//验证不能为空
			$scope.editForm.balanceHoursDay.$dirty=false;
			$scope.editForm.afterBalanceHoursDay.$dirty=false;
			
		};
} ]);